package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.ChangeUser;
import cn.tedu.supermarket.mapper.UserMapper;
import cn.tedu.supermarket.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class ChangePasswordController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmailService emailService;

    @RequestMapping(value = "/change_email",produces = "text/html;charset=utf-8")
    public void start(String mailIndex){
        emailService.email(mailIndex);
    }

    @RequestMapping(value = "/change_check")
    public ModelAndView changBuser(ChangeUser cuser, ModelMap map) {
        System.out.println("验证开始");
        System.out.println(emailService.getCaptcha()+","+cuser.getCaptcha());
        System.out.println(cuser.getMailIndex());
        if(cuser.getUsername()==null||cuser.getUsername()==""||cuser.getMailIndex()==null||cuser.getMailIndex()==""){
            map.put("changepwd","用户名或邮箱不能为空");
            System.out.println("用户名或邮箱不能为空");
            return new ModelAndView("buy_user/buserChangePassword");
        } else if (emailService.getCaptcha().equals(cuser.getCaptcha())){
            System.out.println("验证码正确");
            try {
                int a = userMapper.changeBuyUserPasswordId(cuser.getUsername(),cuser.getMailIndex());
                if (a>0){
                    userMapper.changeBuyUserPassword(cuser.getPassword2(),cuser.getUsername());
                    System.out.println("修改成功");
                    return new ModelAndView("buy_user/buserSuccess");
                }else {
                    map.put("changepwd","修改失败,用户不存在");
                    return new ModelAndView("buy_user/buserChangePassword");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        map.put("changepwd","验证码错误");
        System.out.println("验证码错误");
        return new ModelAndView("buy_user/buserChangePassword");
    }

    @RequestMapping(value = "/change_suser")
    public ModelAndView changeSuser(ChangeUser cuser, ModelMap map) {
        System.out.println("验证开始");
        System.out.println(emailService.getCaptcha()+","+cuser.getCaptcha());
        System.out.println(cuser.getMailIndex());
        if(cuser.getUsername()==null||cuser.getUsername()==""||cuser.getMailIndex()==null||cuser.getMailIndex()==""){
            map.put("changepwd","用户名或邮箱不能为空");
            System.out.println("用户名或邮箱不能为空");
            return new ModelAndView("sel_user/suserChangePassword");
        } else if (emailService.getCaptcha().equals(cuser.getCaptcha())){
            System.out.println("验证码正确");
            try {
                int a = userMapper.changeSerUserPasswordId(cuser.getUsername(),cuser.getMailIndex());
                if (a>0){
                    userMapper.changeSelUserPassword(cuser.getPassword2(),cuser.getUsername());
                    System.out.println("修改成功");
                    return new ModelAndView("sel_user/suserSuccess");
                }else {
                    map.put("changepwd","修改失败,用户不存在");
                    return new ModelAndView("sel_user/suserChangePassword");
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        map.put("changepwd","验证码错误");
        System.out.println("验证码错误");
        return new ModelAndView("sel_user/suserChangePassword");
    }
}
